<?php
/**
 * Author: wl
 * Email: 446399160@qq.com
 * DateTime: 2017/05/01 13:31
 */

namespace app\modules\v1\models;
use yii\db\ActiveRecord;
use yii;

class Kameng extends ActiveRecord{
	
 	private static $m = MCRYPT_TRIPLEDES;
 	
 	/**
 	 * 商户进件
 	 */
 	public function MerchantIn($map){
 		//$map['accountNo'] = self::Encryption($map['accountNo'],self::$deskey);
 		$map['accountNo'] = self::Encryption($map['accountNo'],Yii::$app->params['deskey']);
 		$map['sign'] 	= self::MerchantSign($map);
 		ksort($map);
 		$res  = self::CurlPost(Yii::$app->params['url_merchant_in'], json_encode($map));
 		return $res;	
 	}
 	
 	/**
 	 * 商户进件查询
 	 */
 	public function MerchantQuery($merchantNo){
 		$map = array();
 		//$map['channelName'] = self::$channelname;
 		$map['channelName'] = Yii::$app->params['channelname'];
 		//$map['channelNo'] 	= self::$channelno;
 		$map['channelNo'] 	= Yii::$app->params['channelno'];
 		$map['merchantNo']  = $merchantNo;   //商户编号
 		$map['sign']		= self::MerchantSign($map);
 		$res  = self::CurlPost(Yii::$app->params['url_merchant_query'], json_encode($map));
 		return $res;
 	}
 	
	/**
	 * 订单提交
	 */
 	public function OrderPost($map,$deskey){
 		$map['cardNo']    	= self::Encryption($map['cardNo'],$deskey);          //银行卡号
 		$map['idCardNo']  	= self::Encryption($map['idCardNo'],$deskey);		//身份证号码
 		$map['payerName'] 	= self::Encryption($map['payerName'],$deskey);   	//姓名
 		$map['sign'] 	  	= self::OrderSign($map);
 		unset($map['signkey']);
 		$param_post_string 	= http_build_query($map);
 		$res  = self::CurlPost(Yii::$app->params['url_order_in'], $param_post_string);
 		return $res  ;
 	}
 	
 	
 	/**
 	 * 支付宝订单提交
 	 */
 	public function OrderPostZfb($map,$deskey){
 		$map['cardNo']    	= self::Encryption($map['cardNo'],$deskey);          //银行卡号
 		$map['idCardNo']  	= self::Encryption($map['idCardNo'],$deskey);		//身份证号码
 		$map['payerName'] 	= self::Encryption($map['payerName'],$deskey);   	//姓名
 		$map['sign'] 	  	= self::OrderSign($map);
 		unset($map['signkey']);
 		$param_post_string 	= http_build_query($map);
 		$res  = self::CurlPost(Yii::$app->params['url_order_in_zfb'], $param_post_string);
 		return $res  ;
 	}
 	

 	/**
 	 * 订单查询
 	 */
 	public function OrderQuery($map){
 		$map['sign'] 		= self::OrderSign($map);
 		unset($map['queryKey']);
 		$param_post_string 	= http_build_query($map);
 		$res  = self::CurlPost(Yii::$app->params['url_order_query'], $param_post_string);
 		return $res  ;
 	}
 	
 	/**
 	 * 回调获取数据
 	 */
 	public static function GetOrderCallback(){
 		$data 		= array('name'=>'测试');
 		return $data;
 	}
 	
 	/**
 	 * 进件sign生成
 	 */
 	private static function MerchantSign($map){
 		ksort($map);
 		$center 	= '{"';
 		foreach ($map as $key=>$value){
 			$center .= $key.'":"'.$value.'","';
 		}
 		$length 	= strlen($center)-2;
 		//$new_center = substr($center, 0, $length).'}'.self::$signkey;
 		$new_center = substr($center, 0, $length).'}'.Yii::$app->params['signkey'];
 		$sign 		= md5($new_center);
 		$sign 		= strtoupper($sign);
 		return $sign;
 	}
 
 	/**
 	 * 订单sign生成
 	 */
 	private static function OrderSign($parem){
 		$source 	= implode('#', $parem);
 		$source 	= '#'.$source;
 		$sign   	= md5($source);
 		return $sign;
 	}
 	
 	private static function pkcs5_padding($text, $blocksize){
 		$pad = $blocksize - (strlen($text) % $blocksize);
 		return $text . str_repeat(chr($pad), $pad);
 	}
 	
 	/**
 	 * 3des加密
 	 */
 	private static function Encryption($strinfo,$oldkey){
 		$size = mcrypt_get_block_size(MCRYPT_3DES,'ecb');
 		$strinfo = self::pkcs5_padding($strinfo, $size);
 		$key = str_pad($oldkey,24,'0');
 		$td = mcrypt_module_open(MCRYPT_3DES, '', 'ecb', '');
 		$iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
 		@mcrypt_generic_init($td, $key, $iv);
 		$data = mcrypt_generic($td, $strinfo);
 		mcrypt_generic_deinit($td);
 		mcrypt_module_close($td);
 		$data = base64_encode($data);
 		return $data;
 	}
 	
 	/**
 	 * 模拟提交数据函数
 	 * @param  $url   请求地址
 	 * @param  $data
 	 * @return $info
 	 */
 	public static function CurlPost($url,$data){
 		$user_agent = "Apache-HttpClient/4.3.6";
 		$curl = curl_init();
 		curl_setopt($curl, CURLOPT_URL, $url);
 		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
 		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
 		curl_setopt($curl, CURLOPT_USERAGENT,$user_agent);
 		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
 		curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
 		curl_setopt($curl, CURLOPT_POST, 1);
 		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
 		//curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie_file);
 		curl_setopt($curl, CURLOPT_TIMEOUT, 20); //设置超时限制防止死循环
 		curl_setopt($curl, CURLOPT_HEADER, 0);
 		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 		$info = curl_exec($curl);
 		if (curl_errno($curl)) {
 			echo 'Errno'.curl_error($curl);
 		}
 		curl_close($curl);
 		return $info;
 	}
 	
 	public static function MindateNow(){
 		list($msec, $sec) = explode(' ', microtime());
 		$msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
 		return $msectime;
 	}
 	
 	public function ThreeCurlPost($url,$data=''){
 		$headers = array(
 				"Content-type: application/x-www-form-urlencoded;charset=\"utf-8\"",
 		);
 		
 		$ch = curl_init();
 		curl_setopt($ch, CURLOPT_URL,$url);
 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
 		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 		curl_setopt($ch, CURLOPT_POST, 1);
 		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 		$info = curl_exec($ch);
 		if (curl_errno($ch)) {
 			echo 'Errno'.curl_error($ch);
 		}
 		curl_close($ch);
 		return $info;
 	}
}